<template>
  <div class="box" style="position: relative;">
    <input
      type="text"
      placeholder="请输入待办事项"
      class="things"
      v-model="contents"
      @keyup.enter="toDo"
    >
    <div class="no" v-show="toDoThings.length === 0">无待办事项</div>
    <ul>
      <li v-for="(item, index) in toDoThings" :key="item.id">
        <input type="checkbox" class="selection" v-model="item.checked">
        <div :class="item.listClass">{{ item.content }}</div>
        <div class="delete" v-show="item.isShow" @click="del(index)">×</div>
      </li>
    </ul>
    <div class="buttoms">
      <div style="cursor: pointer;" v-show="toDoThings.length > 0">{{ counts }} items left</div>
      <div @click="clear" style="cursor: pointer;" v-show="toDoThings.length > 0">Clear completed</div>
      <div @click="selectAll" v-show="toDoThings.length > 0">全选</div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref, watch, onMounted } from 'vue'

const contents = ref('') // 输入框
const toDoThings = ref([]) 

// 挂载时从本地存储获取数据添加到待办事项
onMounted(() => {
    let results = JSON.parse(localStorage.getItem('toDothing')? localStorage.getItem('toDothing') : '[]')
    toDoThings.value = results
  })

// 回车添加输入框内容
const toDo = () => {
  console.log('触发事件')
  toDoThings.value.push({ id: Date.now(), content: contents.value, checked: false, listClass: 'list', isShow: false })
  contents.value = ''
}

// 监听复选框，
watch(toDoThings, (newToDoThings) => {
  // 改变li的样式和删除的显隐
  newToDoThings.forEach((item) => {
    if (item.checked) {
      // 如果复选框被勾选，改变文字样式为 list2
      item.listClass = 'list2'
      // 显示删除按钮
      item.isShow = true
    } else {
      // 如果复选框未被勾选，恢复文字样式为 list
      item.listClass = 'list'
      // 隐藏删除按钮
      item.isShow = false
    }
  })
  // 持久化到本地存储
  localStorage.setItem('toDothing', JSON.stringify(newToDoThings))
}, { deep: true })

// 删除待办事件
const del = (index) => {
  toDoThings.value.splice(index, 1)
}

// 计算已完成事件的数量
const counts = computed(() => {
   const checkedThings = toDoThings.value.filter((item) => !item.checked)
  return checkedThings.length
})

// 删除所有已勾选的待办事件
const clear = () => {
  toDoThings.value = toDoThings.value.filter((item) =>!item.checked)
  return toDoThings.value
}

// 全选
let isAll = false
const selectAll = () => {
  isAll = !isAll
  toDoThings.value.forEach(item => {
    item.checked = isAll
  })

}

</script>

<style scoped>
* {
  padding: 0;
  margin: 0;
}

.box {
  height: 500px;
  width: 400px;
  padding: 20px;
  border: 2px solid black;
  position: relative;
}

.things {
  height: 40px;
  width: 390px;
  font-size: 20px;
  font-weight: bold;
  border: 2px solid black;
}

ul {
  height: 430px;
  width: 390px;
  overflow-y: auto;
}

li {
  height: 50px;
  width: 350px;
  margin: 10px 0;
  list-style-type: none;
  display: flex;
  align-items: center;
}

.no {
  height: 50px;
  width: 380px;
  text-align: center;
  line-height: 50px;
  color: #ccc;
  font-size: 25px;
  font-weight: bold;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 1;
}

.list {
  color: black;
  font-size: 25px;
  font-weight: bold;
}

.list2 {
  color: #ccc;
  font-size: 25px;
  font-weight: bold;
  text-decoration: line-through;
}

.selection {
  height: 20px;
  width: 20px;
  cursor: pointer;
}

.delete {
  font-size: 40px;
  cursor: pointer;
}

.buttoms {
  display: flex;
  justify-content: space-between;
}
</style>
